package com.zizo.autohome.word.flowtrend.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.zizo.autohome.emotion.alg.AhoCorasickDoubleArrayTrie;
import com.zizo.util.ConnectionPool;

public class Test {
	private AhoCorasickDoubleArrayTrie<Integer> acdatLocal;
	private Map<Integer, List<String>> wordMap;
	private Map<Integer, List<Integer>> idMap;

	public Test(AhoCorasickDoubleArrayTrie<Integer> acdatLocal, Map<Integer, List<String>> wordMap,
			Map<Integer, List<Integer>> idMap) {
		super();
		this.acdatLocal = acdatLocal;
		this.wordMap = wordMap;
		this.idMap = idMap;
	}

	/**
	 * @Title: checkSub
	 * @Description: 去除子集操做
	 * @param list
	 * @return
	 * @return List<Integer>
	 * @author Alexis
	 */
	public List<Integer> checkSub(List<Integer> list) {
		List<Integer> res = new ArrayList<>();
		res.addAll(list);
		for (int i = 0; i < list.size(); i++) {
			for (int j = i + 1; j < list.size(); j++) {
				List<String> s1 = wordMap.get(list.get(i));
				List<String> s2 = wordMap.get(list.get(j));
				int l1 = 0;
				int l2 = 0;
				StringBuffer sb1 = new StringBuffer();
				StringBuffer sb2 = new StringBuffer();
				for (String s : s1) {
					l1 += s.length();
					sb1.append(s);
				}
				for (String s : s2) {
					l2 += s.length();
					sb2.append(s);
				}
				if (l1 >= l2) {
					boolean flag = true;
					String c = sb1.toString();
					for (String s : s2) {
						flag = c.contains(s);
						if (!flag) {
							break;
						}
					}
					if (flag) {
						res.remove((Integer) list.get(j));
					}
				} else {
					boolean flag = true;
					String c = sb2.toString();
					for (String s : s1) {
						flag = c.contains(s);
						if (!flag) {
							break;
						}
					}
					if (flag) {
						res.remove((Integer) list.get(i));
					}
				}
			}
		}
		return res;
	}

	public List<List<String>> getCarAndAlias() {
		List<List<String>> list = new ArrayList<>();
		Connection conn198 = ConnectionPool.getConnection("autohome_other", "localhost");
		Statement st = null;
		ResultSet rs = null;
		String sql = "SELECT CarID,CarName,Alias_1,Alias_2,Alias_3,Alias_4,BizID,ClubID,id FROM brand_models_alias_new";
		try {
			st = conn198.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				int carid = rs.getInt("CarID");
				String carname = rs.getString("CarName");
				int bizid = rs.getInt("BizID");
				int clubid = rs.getInt("ClubID");
				int id = rs.getInt("id");
				String w1 = rs.getString(3);
				String w2 = rs.getString(4);
				String w3 = rs.getString(5);
				String w4 = rs.getString(6);
				if (w1 == null || w1.equals("")) {
					w1 = "";
				}
				if (w2 == null || w2.equals("")) {
					w2 = "";
				}
				if (w3 == null || w3.equals("")) {
					w3 = "";
				}
				if (w4 == null || w4.equals("")) {
					w4 = "";
				}
				List<String> l = new ArrayList<>();
				l.add(id + "");
				l.add(bizid + "");
				l.add(carid + "");
				l.add(clubid + "");
				l.add(carname);
				l.add(w1);
				l.add(w2);
				l.add(w3);
				l.add(w4);
				list.add(l);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static void main(String[] args) {
		Test test = new Test(null, null, null);
		List<List<String>> list = test.getCarAndAlias();
		Map<String, Integer> aliasMap = new HashMap<>();
		Map<Integer, List<Integer>> idMap = new HashMap<>();
		Map<Integer, List<String>> wordMap = new HashMap<>();
		for (List<String> alias : list) {
			int id = Integer.parseInt(alias.get(0));
			int bizid = Integer.parseInt(alias.get(1));
			int carid = Integer.parseInt(alias.get(2));
			int clubid = Integer.parseInt(alias.get(3));
			String w1 = alias.get(5);
			String w2 = alias.get(6);
			String w3 = alias.get(7);
			String w4 = alias.get(8);
			List<String> st = new ArrayList<>();
			if (w1.length() > 0) {
				st.add(w1);
				if (aliasMap.containsKey(w1)) {
					int idMapKey = aliasMap.get(w1);
					List<Integer> l = idMap.get(idMapKey);
					l.add(id);
					idMap.put(idMapKey, l);
				} else {
					aliasMap.put(w1, id);
					List<Integer> l = new ArrayList<>();
					l.add(id);
					idMap.put(id, l);
				}
			}
			if (w2.length() > 0) {
				st.add(w2);
				if (aliasMap.containsKey(w2)) {
					int idMapKey = aliasMap.get(w2);
					List<Integer> l = idMap.get(idMapKey);
					l.add(id);
					idMap.put(idMapKey, l);
				} else {
					aliasMap.put(w2, id);
					List<Integer> l = new ArrayList<>();
					l.add(id);
					idMap.put(id, l);
				}
			}
			if (w3.length() > 0) {
				st.add(w3);
				if (aliasMap.containsKey(w3)) {
					int idMapKey = aliasMap.get(w3);
					List<Integer> l = idMap.get(idMapKey);
					l.add(id);
					idMap.put(idMapKey, l);
				} else {
					aliasMap.put(w3, id);
					List<Integer> l = new ArrayList<>();
					l.add(id);
					idMap.put(id, l);
				}
			}
			if (w4.length() > 0) {
				st.add(w4);
				if (aliasMap.containsKey(w4)) {
					int idMapKey = aliasMap.get(w4);
					List<Integer> l = idMap.get(idMapKey);
					l.add(id);
					idMap.put(idMapKey, l);
				} else {
					aliasMap.put(w4, id);
					List<Integer> l = new ArrayList<>();
					l.add(id);
					idMap.put(id, l);
				}
			}
			int[] ids = new int[3];
			ids[0] = bizid;
			ids[1] = carid;
			ids[2] = clubid;
			if (!wordMap.containsKey(id)) {
				wordMap.put(id, st);
			}
		}
		/* 双Trie树 */
		AhoCorasickDoubleArrayTrie<Integer> acdatLocal = new AhoCorasickDoubleArrayTrie<Integer>();
		acdatLocal.build(aliasMap);
		Test t = new Test(acdatLocal, wordMap, idMap);
		t.test("GLC260豪华型（");
	}

	public void test(String str) {
		List<Integer> ids = acdatLocal.parseTextNotHit(str);
		if (ids.size() == 0) {
			return;
		}
		Set<Integer> set = new HashSet<>();
		for (int s : ids) {
			Set<Integer> ll = new HashSet<>(idMap.get(s));
			set.addAll(ll);
		}
		List<Integer> idList = new ArrayList<>(set);
		List<Integer> idTemp = new ArrayList<>(idList);
		for (int i : idTemp) {
			List<String> word = wordMap.get(i);
			StringBuffer sb = new StringBuffer();
			for (String ss : word) {
				sb.append(ss + "   ");
			}
			boolean flag = true;
			for (String w : word) {
				flag = str.contains(w);
				if (!flag) {
					break;
				}
				// System.out.println(str + " " + w + " " + i + " " + flag);
			}
			if (!flag) {
				idList.remove((Integer) i);
			}
		}
		for (int i : idList) {
			StringBuffer sb = new StringBuffer();
			for (String ww : wordMap.get(i)) {
				sb.append(ww + "  ");
			}
		}
		List<Integer> hitList = checkSub(idList);
		for (int i : hitList) {
			StringBuffer sb = new StringBuffer();
			for (String ww : wordMap.get(i)) {
				sb.append(ww + "  ");
			}
			System.out.println(sb.toString());
		}
	}
}